package com.yonyou.ucf.mdf.app.isvdxq.other.service;

import com.yonyou.ucf.mdf.app.isvdxq.other.mapper.impl.ProductSpecMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.yonyisv.model.EventContent;
import com.yonyou.ucf.mdf.app.isvdxq.other.entity.ProductSpecEntity;
import com.yonyou.ucf.mdf.app.isvdxq.other.model.ContentBean;
import com.yonyou.ucf.mdf.app.isvdxq.utils.RequestTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class ProductSpecService {
    @Autowired
    private ProductSpecMapperImpl productSpecEntityMapper;

    public ProductSpecEntity SelectById(Long Id) {
        return productSpecEntityMapper.selectById(Id);
    }

    public Boolean CheckEntity(Long Id) {
        int rInt = productSpecEntityMapper.selectCount(Id);
        if (rInt > 0) return true;
        else return false;
    }

    public Integer InsertEntity(ProductSpecEntity entity) {
        return productSpecEntityMapper.insert(entity);
    }

    public Integer UpdateEntity(ProductSpecEntity entity) {
        return productSpecEntityMapper.updateByPrimaryKey(entity);
    }

    public List<ProductSpecEntity> SelectAll(String productTemplate, String tenantId) {
        return productSpecEntityMapper.selectAll(productTemplate, tenantId);
    }

    public Integer DeleteById(Long Id) {
        return productSpecEntityMapper.deleteByPrimaryKey(Id.toString());
    }


    //新增并更新
    public Integer InsertBatch(List<ProductSpecEntity> list) {
        if (list.size() == 0) return 0;
        long start = System.currentTimeMillis();
        int res = productSpecEntityMapper.insertBatch(list);
        long end = System.currentTimeMillis();
        System.out.println("---------------" + (start - end) + "---------------");
        return res;
    }

    public Integer EventEdit(EventContent content) throws IOException {
        System.out.println(content.getContent());
        ContentBean obj = RequestTool.getBean(content.getContent(), ContentBean.class);
        String jsonString = obj.getArchive().replace("detail!id", "detailid").replace("detail!", "");
        ProductSpecEntity productSpecEntity = RequestTool.getBean(jsonString, ProductSpecEntity.class);
        productSpecEntity.setTenant_id(obj.getTenantId());
        Boolean flag = CheckEntity(productSpecEntity.getId());
        if (flag) {
            int rInt = UpdateEntity(productSpecEntity);
            return rInt;
        } else {
            int rInt = InsertEntity(productSpecEntity);
            return rInt;
        }
    }

    public Integer EventDel(EventContent content) throws IOException {
        System.out.println(content.getContent());
        ContentBean obj = RequestTool.getBean(content.getContent(), ContentBean.class);
        Long Id = Long.parseLong(obj.getId());
        Boolean flag = CheckEntity(Id);
        if (flag) {
            int rInt = DeleteById(Id);
            return rInt;
        }
        return 0;
    }
}
